"""
Case Type   : datakit安全中心
Case Name   : 其它账号修改角色权限范围
Create At   : 2025/2/7
Owner       : @sungang14
Description :
    1. 打开安全中心-角色菜单页面
    2. 点击修改某个角色
    3. 将状态改为“停用”
    4. 使用“测试账号”登录查看菜单权限【结果1】
Expect      :
    1、【结果1】发现“测试账号”没有任何菜单权限
History     :
"""

import os
import unittest
import json
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.datakit_api.Api import Api


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.com_sh = CommonSH()
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.sys_api = Api(self.token)
        self.user_name = 'sgtest_user0018'
        self.data_user = f"userName=" + self.user_name
        self.role_name = 'sgtest_role0018'
        self.data_role = f"pageNum=1&pageSize=10&roleName=" + self.role_name

        text = '----查询待添加账号是否存在，存在则删除----'
        self.log.info(text)
        res1 = self.sys_api.query_user_page(self.data_user)
        self.log.info(res1.json())
        self.num_user = res1.json()['total']
        self.log.info(self.num_user)
        if self.num_user != 0:
            self.user_id = res1.json()['rows'][0]['userId']
            res2 = self.sys_api.delete_user([self.user_id])
            self.log.info(res2.json())
            self.assertEqual(200, res2.json()['code'], '删账号失败')
            self.assertEqual("success", res2.json()['msg'], '删除账号失败')
        text = '----查询待添加角色是否存在，存在则删除----'
        self.log.info(text)
        res1 = self.sys_api.query_role_list(self.data_role)
        self.log.info(res1.json())
        self.num_role = res1.json()['total']
        self.log.info(self.num_role)
        if self.num_role != 0:
            self.role_id = res1.json()['rows'][0]['roleId']
            res7 = self.sys_api.delete_role([self.role_id])
            self.log.info(res7.json())
            self.assertEqual(200, res7.json()['code'], '删除角色失败')
            self.assertEqual("success", res7.json()['msg'], '删除用户失败')

        text = '----安全中心-角色与权限菜单，点击“添加角色”，选择全部菜单权限，点击确定----'
        self.log.info(text)
        self.data1 = {
            "menuIds": [2, 201, 203, 204, 208, 1008, 1009, 1010, 1011, 5, 7, 701, 702, 1024, 1025, 1026, 1027, 1028,1029,
                        1020, 1021, 1022, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1032, 1052, 1049, 1037, 1033,
                        1035, 1038, 1039, 1041, 1042, 1043, 1044, 1045, 1046, 1003, 1004, 1005, 1030, 1031, 1000, 1001,
                        1002, 1047, 1048, 2, 201, 203, 204, 208, 1008, 1009, 1010, 1011, 5, 7, 701, 702, 1024, 1025,1026,
                        1027, 1028, 1029, 1020, 1021, 1022, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1032, 1052,
                        1049, 1037, 1033, 1035, 1038, 1039, 1041, 1042, 1043, 1044, 1045, 1046, 1003, 1004, 1005, 1030,
                        1031, 1000, 1001, 1002, 1047, 1048],
            "roleName": self.role_name,
            "status": f"0",
            "remark": ""}
        res1 = self.sys_api.add_role(self.data1)
        self.log.info(res1.json())
        self.assertEqual(200, res1.json()['code'], '添加角色失败')
        self.assertEqual("success", res1.json()['msg'], '添加角色失败')

        text = '----查询添加角色id----'
        self.log.info(text)
        res2 = self.sys_api.query_role_list(self.data_role)
        self.log.info(res2.json())
        self.assertEqual(1, res2.json()['total'], '添加角色失败')
        self.role_id = res2.json()['rows'][0]['roleId']
        self.log.info(self.role_id)

        text = '----安全中心-账号管理，点击“添加账号”----'
        self.log.info(text)
        self.data3 = {"userName": self.user_name,
                      "password": f"sBfBGhAg4+NypfvYDSAdyvZPx/PAW0NInwVdN3IGNHfj62ltrvonFdhGFEti8MIRajnN0mlbUsUktMOlgIHBWih4ffAEaDcCI+uN1jrQAigQcrOLwNZHkb3pRfQyelWpx2A5LgEhuGmJ6vPN13DSd+C3D5uMGZhGG7BEQPSGomk=",
                      "nickName": self.user_name,
                      "phonenumber": f"16666666666",
                      "roleIds": self.role_id,
                      "status": "0"}
        res3 = self.sys_api.add_user(self.data3)
        self.log.info(res3.json())
        self.assertEqual(200, res3.json()['code'], '添加账号失败')
        self.assertEqual("success", res3.json()['msg'], '添加账号失败')

    def test_batch_installation(self):
        text = '----step1:安全中心-角色与权限菜单，确定对应角色状态为‘停用’ expect:修改成功----'
        self.log.info(text)
        self.data3 = {"roleId": int(self.role_id),
                      "status": f"1"}
        res3 = self.sys_api.update_role_status(self.data3)
        self.log.info(res3.json())
        self.assertEqual(200, res3.json()['code'], '修改角色状态失败')
        self.assertEqual("success", res3.json()['msg'], '修改角色状态失败')

        text = '----step2:使用新创建账号登录 expect:无任何菜单权限----'
        self.log.info(text)
        self.token1 = self.com_sh.get_token_value(password="Huawei@123", user=self.user_name)
        self.sys_api1 = Api(self.token1)
        res4 = self.sys_api1.get_get_routers()
        self.log.info(res4.json())
        self.assertEqual(200, res4.json()['code'], '添加账号失败')
        self.assertEqual("success", res4.json()['msg'], '添加账号失败')
        self.log.info(res4.json()['data'])
        self.assertEqual([], res4.json()['data'])

    def tearDown(self):
        text = '----:查询账号user_id expect:查询成功----'
        self.log.info(text)
        res5 = self.sys_api.query_user_page(self.data_user)
        self.log.info(res5.json())
        self.assertEqual(1, res5.json()['total'], '查询账号失败')
        self.user_id = res5.json()['rows'][0]['userId']
        self.log.info(self.user_id)
        text = '----:删除账号 expect：删除成功----'
        self.log.info(text)
        self.data6 = [self.user_id]
        res6 = self.sys_api.delete_user(self.data6)
        self.log.info(res6.json())
        self.assertEqual(200, res6.json()['code'], '删账号失败')
        self.assertEqual("success", res6.json()['msg'], '删除账号失败')
        text = '----:删除角色 expect：删除成功----'
        self.log.info(text)
        self.data7 = [self.role_id]
        res7 = self.sys_api.delete_role(self.data7)
        self.log.info(res7.json())
        self.assertEqual(200, res7.json()['code'], '删除角色失败')
        self.assertEqual("success", res7.json()['msg'], '删除用户失败')
        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
